import { createRouter, createWebHashHistory } from 'vue-router'

import Layout from '@/views/home/Layout.vue'

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path:'/',
      name:'home',
      component: Layout,
      children:[
        // 个人中心
        {
          path: "/personal",
          component: () => import("@/views/home/Personal.vue"),
        },
        {
          path:'/employee',
          component:()=>import('@/views/emp/Employee.vue')
        },{
          path:'/business',
          component:()=>import('@/views/business/BusinessList.vue')
        },{
          path:'/shopAdd',
          component:()=>import('@/views/shop/ShoppingAdd.vue')
        },{
          path:'/shopList',
          component:()=>import('@/views/shop/ShoppingList.vue')
        },{
          path:'/category',
          component:()=>import('@/views/category/ShopCategory.vue')
        },{
          path:'/shopStatus',
          component:()=>import('@/views/business/ShopStatusChange.vue')
        },
         // 菜品分类
        {
          path: "/foodCategory",
          component: () => import("@/views/food/FoodCategory.vue"),
        },
        // 菜品
        {
          path: "/food",
          component: () => import("@/views/food/Food.vue"),
        }
      ]
    },
    {
      path:'/register',
      component: ()=>import('@/views/business/RegisterBusiness.vue')
    },
    {
      path:'/login',
      component: ()=>import('@/views/business/LoginBusiness.vue')
    },
    // 平台员工
    {
      path: "/empLogin",
      name: "empLogin",
      component: () => import("@/views/emp/EmpLogin.vue"),
    }
  ]
})

router.beforeEach((to, from ,next) => {
  // 获取登录信息
  let userInfo = sessionStorage.getItem("businessInfo");

  //权限校验123
  // let urls = ["/login","/empLogin",'/register'];

  if(!userInfo){
    if(!(to.path == '/login' ||to.path=='/empLogin'||to.path=='/register')){
      router.push({path:'/login'});
    }
  }else{
    if(to.path.includes('/login')){
      if(confirm("请确定要退出登录吗?")){
        sessionStorage.removeItem("businessInfo");
      }
    }
  }
  next()
})

export default router
